<?php

Globals::requireClass('Controller');
Globals::requireTable('Group');
Globals::requireTable('User');
Globals::requireTable('Acl');
Globals::requireTable('Groupacl');

class AclController extends Controller {

    protected $Group;
    protected $User;
    protected $Acl;
    protected $GroupaclTable;
    public static $defaultConfig = array(
        'viewEnabled' => true,
        'layoutEnabled' => true,
        'title' => null
    );

    public function __construct($config = null) {
        parent::__construct($config);
        $this->GroupTable = new GroupTable($config);
        $this->UserTable = new UserTable($config);
        $this->AclTable = new AclTable($config);
        $this->GroupaclTable = new GroupaclTable($config);  
    }

    public function indexAction() {
        $this->config['layoutEnabled'] = false;
    }

    public function grouplistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $name = $this->getParam('search_name');
        $where = "`status` > 0";
        if (!empty($name)) {
            $where .= " and name like '%{$name}%'";
            $this->view->name = $name;
        }
        $re = $this->GroupTable->listall($where);
        $this->view->data = $re;
       $acl = $this->AclTable->listall("`status` > 0");
       $this->view->acl = $acl;
        $this->config['layoutEnabled'] = false;
    }
    
    public function groupacljsonAction(){
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $id = $this->getIntParam('id');
        $data = $this->GroupaclTable->listAll("`group_id` = $id");
        echo json_encode($data);
        exit;
    }

    public function groupaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $name = $this->getParam('name');
        $aclid = $this->getParam('acl_id');
        $re = $this->GroupTable->listall();
        foreach ($re as $k => $v) {
            if ($v['name'] == $name) {
                echo 0;
                exit;
            }
        }


        $data = array(
            'name' => $name
        );
        $re = $this->GroupTable->add($data,true);
        if($re){
         foreach($aclid as $k=>$v){
           $this->GroupaclTable->add(array("`group_id`"=>$re,"`acl_id`"=>$v));
         }
         echo $re;
         exit;
        }else{
            echo -1;
            exit;
        }
     
    }

    public function groupupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $id = $this->getParam('id');
        $acl_id = $this->getParam('acl_id');
        $where = "id != '{$id}'";
        $re = $this->GroupTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $name) {
                echo 0;
                exit;
            }
        }

        $where2 = "id = '{$id}'";
        $data = array(
            'name' => $name,
        );
        $re = $this->GroupTable->update($data, $where2);
        $this->GroupaclTable->delete("`group_id` = $id");
        if(count($acl_id)){
            foreach($acl_id as $k=>$v){
            $this->GroupaclTable->add(array("`group_id`"=>$id,"`acl_id`"=>$v));
            }
        }
         echo $id;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function dogroupdelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->GroupTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function acllistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('search_name');
        $where = "1 = 1";
        if (!empty($name)) {
            $where .= " and name like '%{$name}%'";
            $this->view->name = $name;
        }
        $re = $this->AclTable->listall($where);
        $this->view->data = $re;

        $this->config['layoutEnabled'] = false;
    }

    public function acladdAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');

        $re = $this->AclTable->listall();
        foreach ($re as $k => $v) {
            if ($v['name'] == $name) {
                echo 0;
                exit;
            }
        }


        $data = array(
            'name' => $name,
            'status' => 2,
        );
        $re = $this->AclTable->add($data);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function aclupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $id = $this->getParam('id');

        $where = "id != '{$id}'";
        $re = $this->AclTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $name) {
                echo 0;
                exit;
            }
        }

        $where2 = "id = '{$id}'";
        $data = array(
            'name' => $name,
        );
        $re = $this->AclTable->update($data, $where2);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doacldelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->AclTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function userlistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('search_name');
        $status = $this->getParam('search_type');
        $where = "1 = 1";
        $where2 = "1 = 1";
        if (!empty($name)) {
            $where .= " and username like '%{$name}%'";
            $this->view->name = $name;
        }

        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $where2 .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $where2 .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
            $where2 .= " and status > 0";
            $this->view->status = 3;
        }

        $group = $this->GroupTable->listall($where2);
        $this->view->group = $group;

        $re = $this->UserTable->listall($where);
        
        foreach ($re as $k => $v) {
            $re[$k]['group_name'] = '';
            foreach ($group as $kk => $vv) {
                if ($v['group_id'] == $vv['id']) {
                    $re[$k]['group_name'] = $group[$kk]['name'];
                }
            }
        }
        $this->view->data = $re;

        $this->config['layoutEnabled'] = false;
    }

    public function useraddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $password = $this->getParam('password');
        $group_id = $this->getParam('group_id');

        $re = $this->UserTable->listall();
        foreach ($re as $k => $v) {
            if ($v['username'] == $name) {
                echo 0;
                exit;
            }
        }

        $time = date("Y-m-d H:i:s", time());

        $data = array(
            'username' => $name,
            'password' => md5($password),
            'group_id' => $group_id,
            'createtime' => $time,
        );
        $re = $this->UserTable->add($data);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function userupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $password = $this->getParam('password');
        $group_id = $this->getParam('group_id');
        $id = $this->getParam('id');

        $where = "id != '{$id}'";
        $re = $this->UserTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['username'] == $name) {
                echo 0;
                exit;
            }
        }

        $where2 = "id = '{$id}'";
        $data = array(
            'username' => $name,
            'group_id' => $group_id,
        );
        if ($password != '') {
            $data = array(
                'username' => $name,
                'password' => md5($password),
                'group_id' => $group_id,
            );
        }
        $re = $this->UserTable->update($data, $where2);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function douserdelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->UserTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    protected function out() {
        $this->layout->nav = 'index';
        parent::out();
    }

}

Config::extend('AclController', 'Controller');


